/* GM REPLACEMENT FUNCTIONS */

if (typeof(GM_getValue) == 'undefined')
{
	// GM_get/setValue based on pup's cookie code again
	GM_getValue = function(name, defaultValue) 
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) 
		{
			begin = dc.indexOf(prefix);
			if (begin != 0) return defaultValue;
		}
		else
		{
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
		{
			end = dc.length;
		}
		return unescape(dc.substring(begin + prefix.length, end));
	};
}

if (typeof(GM_setValue) == 'undefined')
{
	GM_setValue = function (name, value, future) 
	{
		if (future == null) 
		{
			future = 5;
		}
		var tempdate = new Date();
		tempdate = tempdate.getTime() + ((future - 1 + future) * (7 * 24 * 60 * 60 * 1000));
		var cookieDate = new Date(tempdate);
		var curCookie = name + "=" + escape(value) + "; expires=" + cookieDate.toGMTString() + "; path=/";
		document.cookie = curCookie;
	};
}

if (typeof(GM_addStyle) == 'undefined')
{
	// thx http://meyerweb.com/eric/thoughts/2004/11/10/s5-11a2/
	GM_addStyle = function (string)
	{
		if(/microsoft/i.test(navigator.appName) && !/opera/i.test(navigator.userAgent))
		{
			document.createStyleSheet().cssText=string;
		}
		else 
		{
			var ele=document.createElement('link');
			ele.rel='stylesheet';
			ele.type='text/css';
			ele.href='data:text/css;charset=utf-8,'+escape(string);
			document.getElementsByTagName('head')[0].appendChild(ele);
		}
	}; 
}

if (typeof(GM_log) == 'undefined')
{
	GM_log = function(str) 
	{ 
		contentWindow.console.log(str); 
	}; 
}
/* END GM REPLACEMENT FUNCTIONS */
/* OPTIONS */
function script(name, description, enabledDefault) {
    this.name = name;
    this.description = description;
    this.enabledDefault = enabledDefault;
}

/*
 * Utilities function cause doing this is boring. Who needs jquery anyway? :P
 */
function S(id) {
    return contentWindow.document.getElementById(id);
}

ported_scripts =
	{
	  	'post_preview': new script('Post Preview', 'Adds a Preview button next to Post. Preview your posts before posting.', "true")
	}
	
contentWindow.toggle_script = function(script_key, enabled) 
{
    GM_setValue(script_key, enabled);
}

// if the script is in the off by default array, then the user has to turn it on
contentWindow.script_enabled = function(script_key) {
    return GM_getValue(script_key, ported_scripts[script_key].enabledDefault);
}

if (document.getElementById('commentssettings') != null) 
{
	script_options = document.createElement('div');
	script_options.setAttribute("id", "shackscripts_options");
	script_options.style.clear = 'both'; 
	script_options_html = "<div class=\"interiorform\"><h5>Shackscripts Extension Settings</h5>";
	
	var i = 0; 
	
	for (key in ported_scripts) 
	{
		var classPosition = (i % 2 == 0) ? 'first' : 'last';
		i++;
		var isChecked = (contentWindow.script_enabled(key) == 'true') ? ' checked="checked"' : '';
		script_options_html += '<div class="control condense ' + classPosition + '"><label for="toggle_' + key + '" title="' + ported_scripts[key].description + '"><input type="checkbox" ' + isChecked + '" id="toggle_' + key + '" onclick="toggle_script(\'' + key + '\',this.checked);" />' + ported_scripts[key].name + '</label></div>';
	}
	
	script_options_html += '</div>'; // /interiorform
	
	script_options_html += '<div id="shackscripts_links" style="clear: both;"><ul><li><a href="http://code.google.com/p/shackscripts/wiki/Main">Shackscripts Extension Home</a></li><li><a href="http://code.google.com/p/shackscripts/">Shackscripts Extension Google Code Project</a></li></ul></div>';
	
	script_options.innerHTML = script_options_html
	
	document.getElementById('commentssettings').appendChild(script_options);
}
	
/* END OPTIONS */
